﻿using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace HCMS.HttpApi.Site.Filter
{
    /// <summary>
    /// 添加公共请求参数 过滤器
    /// </summary>
    /// <remarks>
    /// 每个请求接口，都需要 access_token 公共参数
    /// </remarks>
    public class AddCommonParametersOperationFilter : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            // 认证控制器，不需要公共参数
            if (operation.Tags.FirstOrDefault(t => t.Name == "OAuth2") != null) { return; }

            if (operation.Parameters == null)
            { 
                operation.Parameters = new List<OpenApiParameter>();
            }

            operation.Parameters.Add(new OpenApiParameter
            {
                Name = "access_token",
                In = ParameterLocation.Query,
                Description = "授权参数，请先从 OAuth 2.0 获取",
                Required = true,
                Schema = new OpenApiSchema { Type = "string" }
            });
        }
    }
}
